Ext.define('Wgo.controller.Main', {
    extend: 'Ext.app.Controller',
    views: ['Login'],
    stores: ['FestivalStore'],
    config: {
        refs: {
            main: 'mainpanel',
            exhibitionlist: 'exhibitionlist',
            loginForm:'#loginForm' // <anyKey> : <object/dom identifier> this will freely create a function called get<AnyKey>()
                                   // We can also use xtype i.e. loginForm:'login'
            },
        control: { //Uses ComponentQuery selector to find the matching
            'exhibitionlist': {disclose: 'showDetail'},
            'button[action=btnLoginSubmit]' : {tap:"submitLoginForm"}
        }
    },
    showDetail: function(list, record) {
        console.log("Eureka");
    },

    init: function() {
        console.log("Main controller init")
        // Provide a means to intercept loads of each page of records
        var threadStore = Ext.getStore('FestivalStore');
        threadStore.addBeforeListener('load', this.checkForThreadListEnd, this);
    },

    //Event Handlers
    submitLoginForm:function(){
        //console.log("login button tap event");
        //console.log(form);
        var form = this.getLoginForm();
        form.submit({
            url:'loginform.aspx',
            success : function(){Ext.Viewport.setActiveItem({xtype:'mainpanel'},{type: 'slide', direction: 'right'});},
            failure: function(){alert("Failure")}
        })
        //Ext.Viewport.setActiveItem({xtype:'mainpanel'},{type: 'slide', direction: 'right'});
        //alert("To prove the calls are asynchronous")
    },
    checkForThreadListEnd: function(store, records, isSuccessful) {
        console.log("checkForThreadListEnd")
        debugger;
        var pageSize = store.getPageSize();
        var pageIndex = store.currentPage - 1;    // Page numbers start at 1

        if(isSuccessful && records.length < pageSize)
        {
            //Set count to disable 'loading' message
            var totalRecords = pageIndex * pageSize + records.length;
            store.setTotalCount(totalRecords);
        }
        else
            store.setTotalCount(null);
    }

});
